#include <string.h>
#include <glib.h>

int main (int argc, char** argv)
{
    GRegex* regex;
    GMatchInfo *match_info;
    GError *error = NULL;
    const gchar *str = "<body><h1>Title</H1>hello:<br><h2>SubTitle1</h2>world.<H2>SubTitle2</H2>foobar.<h2>This is invalid HTML</h3></body>";
    //const gchar *pat = "<[hH][1-6]>.*?</[hH][1-6]>";
    const gchar *pat = "<[hH]([1-6])>.*?</[hH]\\1>";

    regex = g_regex_new(pat, 0, 0, &error);

    const gchar *tmp = NULL;
    tmp = g_regex_get_pattern(regex);
    if(NULL != tmp) {
        g_print("get pattern: %s \n", tmp);
    }

    gint cnt = 0;
    cnt = g_regex_get_max_backref(regex);
    g_print("back reference num is: %d \n", cnt);

    g_print("original str:\n%s\n", str);
    
    g_regex_match(regex, str, 0, &match_info);

    while (g_match_info_matches(match_info)) {
        gchar* word = g_match_info_fetch(match_info, 0);
        g_print("%s \n",word);
        g_free(word);

        g_match_info_next(match_info, NULL);
    }

    g_match_info_free(match_info);
    g_regex_unref(regex);

  return 0;
}